package controllers.manage.recharge;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import models.constants.AvailableStatus;
import models.weixin.amount.RechargeCombo;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by youliangcheng on 17/5/11.
 */
@With(BaseController.class)
public class RechargeComboCtrl extends Controller {

    /**
     * 跳转到会员卡充值页面
     */
    public static void rechargeComboList(Integer pageNumber , Double rechargeAmount , AvailableStatus availableStatus) {

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();

        if(rechargeAmount != null) {
            searchMap.put("rechargeAmount", rechargeAmount);
        }

        if(availableStatus != null){
            searchMap.put("availableStatus", availableStatus);
        }


        JPAExtPaginator<RechargeCombo> resultPage = RechargeCombo.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber , rechargeAmount , availableStatus);
    }


    //禁用
    public static void freezeCombo(long id, Integer pageNumber) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        RechargeCombo rechargeCombo = RechargeCombo.findById(id);
        rechargeCombo.availableStatus = AvailableStatus.FREEZE;
        Secure.setBaseInfo(rechargeCombo);
        rechargeCombo.save();
        flash.put("msg", "禁用成功");
        redirect("/manage/recharge/RechargeComboCtrl/rechargeComboList/" + pageNumber);
    }


    //启用
    public static void availableCombo(long id, Integer pageNumber) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        RechargeCombo rechargeCombo = RechargeCombo.findById(id);
        rechargeCombo.availableStatus = AvailableStatus.AVAILABLE;
        Secure.setBaseInfo(rechargeCombo);
        rechargeCombo.save();
        flash.put("msg", "启用成功");
        redirect("/manage/recharge/RechargeComboCtrl/rechargeComboList/" + pageNumber);
    }


    //充值套餐详情
    public static void rechargeComboDetail(long id , Integer pageNumber ){

        RechargeCombo rechargeCombo = new RechargeCombo() ;
        if(id > 0)
            rechargeCombo = RechargeCombo.findById(id);
        render(rechargeCombo , id , pageNumber);

    }

    public static void saveDetail(RechargeCombo rechargeCombo ,Integer pageNumber ){
        Secure.setBaseInfo(rechargeCombo);
        if(rechargeCombo != null && rechargeCombo.id != null){
            rechargeCombo.update();
        }else if(rechargeCombo != null){
            rechargeCombo.availableStatus = AvailableStatus.AVAILABLE;
            rechargeCombo.save();
        }

//        redirect("/manage/recharge/RechargeComboCtrl/rechargeComboDetail/"+rechargeCombo.id+"/"+pageNumber);
        redirect("/manage/recharge/RechargeComboCtrl/rechargeComboList");

    }


}
